package spring.AOPDemo.BeforeAdviceTest;



import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/*
是时候使用spring来改造我们这些没礼貌的服务员了
 */
public class BeforeAdviceTest {
    private Waiter waiter;
    private BeforeAdvice advice;
    private ProxyFactory pf;
    @BeforeTest
    public void init() {
        waiter = new NavieWaiter();
        advice = new GreetingBeforeAdvice();
        //spring的代理工厂类
        pf = new ProxyFactory();
        //设置代理目标
        pf.setTarget(waiter);
        //为代理类添加增强
        pf.addAdvice(advice);

    }

    @Test
    public void beforeAdviceTest() {
        Waiter proxy = (Waiter)pf.getProxy();
        proxy.greetTo("John");
        proxy.serveTo("Tom");
    }

    @Test
    public void beforeAdviceTestXml() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("advice.xml");
        Waiter waiter = (Waiter)applicationContext.getBean("waiter");
        waiter.greetTo("十七");
        waiter.serveTo("十七");
    }

    @Test
    public void ExceptionAdviceTestXml() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("advice.xml");
        ExceptionDemo exceptionDemo = (ExceptionDemo)applicationContext.getBean("exceptionProxy");
        exceptionDemo.excep(1);
    }
}
